package com.ruoyi.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BusSleepRecord;
import com.ruoyi.system.service.IBusSleepRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 睡眠记录Controller
 *
 * @author wuxiao
 * @date 2025-09-28
 */
@Controller
@RequestMapping("/system/record")
public class BusSleepRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IBusSleepRecordService busSleepRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询睡眠记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusSleepRecord busSleepRecord)
    {
        startPage();
        String beginRecordDate = busSleepRecord.getParams().get("beginRecordDate").toString();
        String endRecordDate = busSleepRecord.getParams().get("endRecordDate").toString();
        if(!"".equals(endRecordDate) && !"".equals(endRecordDate)){
            busSleepRecord.getParams().put("beginRecordDate", beginRecordDate+" 00:00:00");
            busSleepRecord.getParams().put("endRecordDate",  endRecordDate+" 00:00:00");
        }
        List<BusSleepRecord> list = busSleepRecordService.selectBusSleepRecordList(busSleepRecord);
        return getDataTable(list);
    }

    /**
     * 导出睡眠记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "睡眠记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusSleepRecord busSleepRecord)
    {
        List<BusSleepRecord> list = busSleepRecordService.selectBusSleepRecordList(busSleepRecord);
        ExcelUtil<BusSleepRecord> util = new ExcelUtil<BusSleepRecord>(BusSleepRecord.class);
        return util.exportExcel(list, "睡眠记录数据");
    }

    /**
     * 新增睡眠记录
     */
    @RequiresPermissions("system:record:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存睡眠记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "睡眠记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusSleepRecord busSleepRecord)
    {
        return toAjax(busSleepRecordService.insertBusSleepRecord(busSleepRecord));
    }

    /**
     * 修改睡眠记录
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BusSleepRecord busSleepRecord = busSleepRecordService.selectBusSleepRecordById(id);
        mmap.put("busSleepRecord", busSleepRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存睡眠记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "睡眠记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusSleepRecord busSleepRecord)
    {
        return toAjax(busSleepRecordService.updateBusSleepRecord(busSleepRecord));
    }

    /**
     * 删除睡眠记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "睡眠记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busSleepRecordService.deleteBusSleepRecordByIds(ids));
    }
}
